<template>
    <div>
        <el-card>
    <el-form :inline="true">
        <el-form-item>
            <el-select placeholder="请选择类型" v-model="query.type">
                <el-option label="用户名" value="username"></el-option>
                <el-option label="手机号" value="phone"></el-option>
                <el-option label="邮箱" value="email"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-input placeholder="请输入值" v-model="query.value"></el-input>
        </el-form-item>
        <el-form-item>
             <el-button icon="el-icon-search" type="primary" @click="getUsers">搜索</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="list" stripe border>
      <el-table-column prop="username" label="用户名" align="center" />
      <el-table-column prop="phone" label="电话" align="center"/>
      <el-table-column prop="email" label="邮箱" align="center"/>
      <el-table-column prop="auth" label="角色" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.auth==1?'primary':'success'">
                 {{scope.row.auth==1?"超级管理员":scope.row.auth==2?"普通管理员":""}}
            </el-tag>
          </template>
      </el-table-column>
      <el-table-column prop="image" label="头像" align="center">
            <template slot-scope="scope">
                <el-avatar :size="50" :src="scope.row.image"></el-avatar>
            </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
           <template slot-scope="scope">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="updateUser(scope)">编辑</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
           </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
     <el-pagination
       background
       layout="sizes, prev, pager, next, jumper, ->, total, slot"
      :total="total"
      :page-size="query.pageSize"
      :current-page="query.currentPage"
      :page-sizes="[5,10,15,20]"
      @size-change="changePageSize"
      @current-change="changeCurrentPage">
     </el-pagination>
      </el-card>

      <!-- 修改的模态框 -->
      <el-dialog
        title="修改用户"
         :visible.sync="updateDialogVisible"
         width="650px">
          <el-form label-width="60px">
            <el-form-item label="用户名">
                <el-input v-model="updateUserItem.username"></el-input>
            </el-form-item>
             <el-form-item label="电话">
                <el-input v-model="updateUserItem.phone"></el-input>
            </el-form-item>
             <el-form-item label="邮箱">
                <el-input v-model="updateUserItem.email"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="updateUserItem.password"></el-input>
            </el-form-item>
            <el-form-item label="角色">
                <!-- <el-radio-group v-model="updateUserItem.auth">
                  <el-radio label="超级管理员" :value="1"></el-radio>
                  <el-radio label="普通管理员" :value="2"></el-radio>
              </el-radio-group> -->
              <el-select placeholder="选择角色" v-model="updateUserItem.auth">
                <el-option label="超级管理员" :value="1"></el-option>
                <el-option label="普通管理员" :value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="头像">
                <el-input v-model="updateUserItem.image"></el-input>
            </el-form-item>
          </el-form>
           <span slot="footer" class="dialog-footer">
            <el-button @click="updateDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateUserApi">确 定</el-button>
          </span>
      </el-dialog>
    </div>
</template>

<script>
import {createNamespacedHelpers} from 'vuex'
const {mapMutations:mapTabsMutations}=createNamespacedHelpers("tabs")
export default {
  data() {
    return {
      list: [],
      total: 0,
      query:{
        type:'',
        value:'',
        pageSize:5,
        currentPage:1
      },
      updateDialogVisible:false,
      updateUserItem:{
        username:'',
        auth:1
      }
    };
  },
  methods: {
    ...mapTabsMutations(["addTabs"]),
    async getUsers() {
      const result = await this.$api.users.getUsers(this.query);
      console.log("result", result.data);
      this.list = result.data.result;
      this.total = result.data.total;
    },
    changePageSize(args){
      this.query.pageSize=args
      this.getUsers()
    },
    changeCurrentPage(args){
       this.query.currentPage=args
       this.getUsers()
    },
    updateUser(args){
      console.log('args',args.row);
      this.updateUserItem={...args.row}
      //显示模态框
      //this.updateUser.username=args.row.username
      this.updateDialogVisible=true
    },
    async updateUserApi(){
      console.log('修改后的数据',this.updateUserItem);
      const result=await this.$api.users.updateUsers(this.updateUserItem)
      console.log('resultss',result);
      if(result.code){
        this.$message.success(result.message)
        //重新渲染列表页面
        this.getUsers()
        //模态框关闭
        this.updateDialogVisible=false
      }
    }
  },
  created() {
    this.getUsers();
  },
   beforeRouteEnter(to,from,next){
        next(vm=>{
           vm.addTabs(to)
        })
    }
};
</script>

<style>
</style>